<?php

/**
* alltosun.com 分类静态方法 index.php
* ============================================================================
* 版权所有 (C) 2009-2014 北京互动阳光科技有限公司，并保留所有权利。
* 网站地址: http://www.alltosun.com
* ----------------------------------------------------------------------------
* 许可声明: 这是一个开源程序，未经许可不得将本软件的整体或任何部分用于商业用途及再发布。
* ============================================================================
* $Author: 吴松键 (wusj@alltosun.com) $
* $Date: 2014-8-8 下午12:36:18 $
* $Id$
*/

class provincial_helper
{
    /**
     *通过父级id查询分类列表
     * @param int $parent_id
     * @return array()
     */
    public static function get_provincial_list_by_parent_id($parent_id)
    {
        $filter['parent_id'] = (int)$parent_id;
        $filter['status']   = 1;
        return _model('provincial')->getList($filter, " ORDER BY `id` ASC,concat(`path`,'-',`id`) ASC");
    }

    /**
     *通过res_name  查询分类列表
     * @return array()
     */
    public static function get_list_by_res_name($res_name = 'goods')
    {
        $filter['res_name'] = $res_name;
        $filter['status']   = 1;

        return _model('provincial')->getList($filter, " ORDER BY `res_name` DESC,concat(`path`,'-',`id`) ASC");
    }

    /**
     *通过父级id 和 查询所有子级id
     * @param int $parent_id
     * @return array()
     */
    public static function get_children_ids($parent_id)
    {
        $provincial_info = _uri('provincial', $parent_id);
        $path = $provincial_info['path'].'-'.$provincial_info['id'];

        $filter['`path` LIKE '] = $path.'%';

        $provincial_ids = _model('provincial')->getFields('id', $filter);

        return $provincial_ids;
    }

    /**
     *通过分类id 获取分类路径
     * @param int $id
     * @return array()
     */
    public static function get_path_by_id($id)
    {
        $provincial_info = _uri('provincial', (int)$id);
        $path = $provincial_info['path'].'-'.$provincial_info['id'];
        return explode('-', $path);
    }

    /**
     *通过分类id 获取顶级分类id
     * @return int
     */
    public static function get_top_id($provincial_id)
    {
        if (!$provincial_id) {
            return 0;
        }

        $provincial_info = _uri('provincial', $provincial_id);
        if ($provincial_info['parent_id'] == 0) {
            return $provincial_id;
        }

        $path_arr = explode('-', $provincial_info['path']);

        return $path_arr[1];
    }

    public static function get_id_by_title($prov, $city, $dist)
    {
        $info = array();
        if (!$prov) {
            return $info;
        }

        $filter = array(
            'title' => $prov,
            'parent_id' => 0
        );
        $prov_info = _model('provincial')->read($filter);
        if (!$prov_info) {
            return $info;
        }

        $info['prov_id'] = $prov_info['id'];

        if (!$city) {
            return $info;
        }

        $filter = array('title'=>$city, 'parent_id'=>$info['prov_id']);
        $city_info = _model('provincial')->read($filter);
        if (!$city_info) {
            return $info;
        }

        $info['city_id'] = $city_info['id'];

        if (!$dist) {
            return $info;
        }

        $filter = array('title'=>$dist, 'parent_id'=>$info['city_id']);
        $dist_info = _model('provincial')->read($filter);
        if (!$dist_info) {
            return $info;
        }

        $info['dist_id'] = $dist_info['id'];

        return $info;
    }
}
?>